//
//  MenuView.m
//  JKSideSlipView
//
//  Created by Jakey on 15/1/10.
//  Copyright (c) 2015年 www.skyfox.org. All rights reserved.
//

#import "MenuView.h"

//#import "MenuCell.h"
@implementation MenuView
+(instancetype)menuView
{
    UIView *result = nil;
    
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    for (id object in nibView)
    {
        if ([object isKindOfClass:[self class]])
        {
            result = object;
            
            break;
        }
    }
    return result;
}
-(instancetype)init
{
    self=[super init];
    
    return self;
}
- (IBAction)homeBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(menuViewHomeBtnClick)]) {
        
        [self.delegate menuViewHomeBtnClick];
    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        if ([self.delegate respondsToSelector:@selector(menuViewConfirmCancelClick)]) {
            
            [self.delegate menuViewConfirmCancelClick];
        }
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user removeObjectForKey:@"tokenId"];
        
        [user synchronize];
        
    }
    
}
- (void)layoutSubviews{
    [super layoutSubviews];


    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.mas_top);
        make.height.equalTo(@StatusBarAndNavigationBarHeight);
        make.right.equalTo(self.mas_right);
        make.left.equalTo(self.mas_left);
        
    }];
    
    [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top).offset(StatusBarAndNavigationBarHeight);
        make.height.equalTo(@0.5);
        make.right.equalTo(self.mas_right).offset(-15);
        make.left.equalTo(self.mas_left).offset(15);
        
    }];
    
    [self.logoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.topView.mas_bottom);
        make.height.equalTo(@40);
        make.right.equalTo(self.mas_right).offset(-21);
        make.left.equalTo(self.mas_left).offset(21);
        
    }];
}


-(void)didSelectRowAtIndexPath:(void (^)(id cell, NSIndexPath *indexPath))didSelectRowAtIndexPath{
    
    _didSelectRowAtIndexPath = [didSelectRowAtIndexPath copy];
}
-(void)setItems:(NSArray *)items{
    
    parentArray=nil;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    parentArray =[[NSMutableArray alloc]init];
    menus = [user objectForKey:@"menus"];
    if (menus.count>0) {
        for (NSDictionary* dic in menus ){
            [parentArray addObject: [dic objectForKey:@"funName"]];
        }
    }
    
    [_myTableView setDataSourceDelegate:self];
    [_myTableView setTableViewDelegate:self];

}


#pragma mark - SubTableViewDelegate

// @optional

- (void)tableView:(UITableView *)tableView didSelectCellAtChildIndex:(NSInteger)childIndex withInParentCellIndex:(NSInteger)parentIndex {
//
//    if ( [self.delegate respondsToSelector:@selector(didSelectCellAtChildIndex:withInParentCellIndex:)]) {
//
//        [self.delegate didSelectCellAtChildIndex:childIndex withInParentCellIndex:parentIndex];
//    }
    
}
-(void)subTableviewCellSubtractOrAddClick
{
    [self.delegate subTableviwAddOrSubtractClickAtHome];
}
- (void)childDidSelectCellAtChildIndex:(NSInteger)childIndex withInParentCellIndex:(NSInteger)parentIndex {
    if ( [self.delegate respondsToSelector:@selector(didSelectCellAtChildIndex:withInParentCellIndex:)]) {
        
        [self.delegate didSelectCellAtChildIndex:childIndex withInParentCellIndex:parentIndex];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectCellAtChildIndex:(NSInteger)childIndex withInParentCellIndex:(NSInteger)parentIndex {
}
- (void)tableView:(UITableView *)tableView didSelectParentCellAtIndex:(NSInteger)parentIndex {
    
}


#pragma mark - SubTableDataSource - Parent
// @required
- (NSInteger)numberOfParentCells {
    return parentArray.count;
}
- (NSInteger)heightForParentRows {
    return 50;
}

// @optional
- (NSString *)titleLabelForParentCellAtIndex:(NSInteger)parentIndex {
    
    return [NSString stringWithFormat:@"%@",[parentArray objectAtIndex:parentIndex]];
}
- (NSString *)subtitleLabelForParentCellAtIndex:(NSInteger)parentIndex {
    
    return nil;
    
}



#pragma mark - SubTableDataSource - Child

// @required
- (NSInteger)numberOfChildCellsUnderParentIndex:(NSInteger)parentIndex {
    
    //    for(int i=0;i<subArray.count;i++)
    //    {
    //        NSLog(@"subArray[%d]=======%@",parentIndex,subArray[parentIndex]);
    //    }
    return [[[menus  objectAtIndex:parentIndex]objectForKey:@"childrens"]count];
}
- (NSInteger)heightForChildRows {
    
    return 49;
}

// @optional
- (NSString *)titleLabelForCellAtChildIndex:(NSInteger)childIndex withinParentCellIndex:(NSInteger)parentIndex {
    NSArray *oneSubArray=[[menus objectAtIndex:parentIndex]objectForKey:@"childrens"];
    NSString *subTitleStr=[[oneSubArray objectAtIndex:childIndex]objectForKey:@"funName"];
    
    return subTitleStr;
    
    //return [NSString stringWithFormat:@"Child %i",childIndex + 1];
}
- (UIImage *)iconForCellAtChildIndex:(NSInteger)childIndex withinParentCellIndex:(NSInteger)parentIndex {
    
    NSString *vcStr = [[[[menus objectAtIndex:parentIndex] objectForKey:@"childrens" ] objectAtIndex:childIndex] objectForKey:@"funCode"];
    if ([vcStr isEqualToString:@"my:group:mgr"]) {
       
        subIconStr=@"wodeshanghu";
        
    } else if ([vcStr isEqualToString:@"summary:sales:mgr"]){
        
        subIconStr=@"xiaoshougaikuang";
        
    } else if ([vcStr isEqualToString:@"customOrder:sales:mgr"]){
        
        subIconStr=@"mendiankedan";
        
    } else if ([vcStr isEqualToString:@"itemVolume:sales:mgr"]){
        
        subIconStr=@"shangpinxiaoliang";
        
    } else if ([vcStr isEqualToString:@"typeVolume:sales:mgr"]){
        
        subIconStr=@"pinleixiaoliang";
        
    } else if ([vcStr isEqualToString:@"summary:member:mgr"]){
        
        subIconStr=@"huiyuangaikuang";
        
    } else if ([vcStr isEqualToString:@"active:member:mgr"]){
        
        subIconStr=@"huiyuanhuoyuedu";
        
    } else if ([vcStr isEqualToString:@"order:b2b:mgr"]){
        
        subIconStr=@"dingdantongji";
        
    } else if ([vcStr isEqualToString:@"refund:b2b:mgr"]){
        
        subIconStr=@"tuihuotongji";
        
    } else if ([vcStr isEqualToString:@"customOrder:b2b:mgr"]){
        subIconStr=@"kedanfenxi";
        
    }
    
    UIImage *img=[UIImage imageNamed:subIconStr];
    UIImage *tinted = [img rt_tintedImageWithColor:[UIColor colorWithRed:146.0/255.0 green:207.0/255.0 blue:243.0/255.0 alpha:1.0] ];
    
    return tinted;
}
- (NSString *)subtitleLabelForCellAtChildIndex:(NSInteger)childIndex withinParentCellIndex:(NSInteger)parentIndex {
    return @"";
    //   return [NSString stringWithFormat:@"nested under parent %i",parentIndex + 1];
    //    NSArray *oneSubArray=[subArray objectAtIndex:parentIndex];
    //    NSString *subTitleStr=[oneSubArray objectAtIndex:childIndex];
    //    //    if (childCount > 1)
    //    //        return [NSString stringWithFormat:@"%i children", (int)childCount];
    //    //    else if (childCount == 1)
    //    //        return [NSString stringWithFormat:@"%i child", (int)childCount];
    //    //    else
    //    //        return @"No children";
    //    return subTitleStr;
    
}



@end
